<template>
  <div class="roles-page">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-button type="primary" @click="addDialogVisible = true"
        >添加角色</el-button
      >
      <el-table :data="tableData" stripe style="width: 100%" border>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"> </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"> </el-table-column>
        <el-table-column label="操作" width="290px">
          <template slot-scope="{ row }">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editRoleInfoList(row.id)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delRoleList(row.id)"
              >删除</el-button
            >
            <el-button type="warning" icon="el-icon-menu" size="mini"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="修改用户" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="editForm"
        status-icon
        :rules="editRules"
        ref="editForm"
        label-width="100px"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="editForm.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="editForm.roleDesc" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editRoleList(editForm.roleId)"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <el-dialog title="添加用户" :visible.sync="addDialogVisible" width="30%">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="ruleForm.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="ruleForm.roleDesc" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRoleList">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { Message } from "element-ui";
import {
  getRolesListApi,
  editGetRoleListApi,
  editRoleListApi,
  delRoleListApi,
  addRoleListApi,
} from "@/api/roles";
export default {
  name: "roles-page",
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      addDialogVisible: false,
      ruleForm: {
        roleName: "",
        roleDesc: "",
      },
      rules: {
        roleName: [
          {
            required: true,
            message: "请输入角色名称",
            trigger: "blur",
          },
        ],
        roleDesc: [
          {
            required: true,
            message: "请输入角色描述",
            trigger: "blur",
          },
        ],
      },
      editForm: {
        id: null,
        roleName: "",
        roleDesc: "",
      },
      editRules: {
        roleName: [
          {
            required: true,
            message: "请输入角色名称",
            trigger: "blur",
          },
        ],
        roleDesc: [
          {
            required: true,
            message: "请输入角色描述",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getRolesList();
  },
  methods: {
    async getRolesList() {
      const res = await getRolesListApi();
      console.log(res);
      this.tableData = res.data;
    },
    async editRoleInfoList(id) {
      this.dialogVisible = true;
      const res = await editGetRoleListApi(id);
      console.log(res);
      this.editForm = res.data;
    },
    async editRoleList(id) {
      await this.$refs.editForm.validate();
      console.log(this.editForm);
      await editRoleListApi(id, {
        roleName: this.editForm.roleName,
        roleDesc: this.editForm.roleDesc,
      });
      Message.success("修改成功");
      this.getRolesList();
      this.dialogVisible = false;
    },
    async delRoleList(id) {
      await this.$confirm("确认删除");
      await delRoleListApi(id);
      Message.success("删除成功");
      this.getRolesList();
    },
    async addRoleList() {
      this.addDialogVisible = true;
      await this.$refs.ruleForm.validate();
      await addRoleListApi(this.ruleForm);
      Message.success("添加成功");
      this.getRolesList();
      this.addDialogVisible = false;
    },
  },
};
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 30px;
  text-align: left;
  .el-table {
    margin-top: 20px;
  }
}
</style>
